package com.bond.jumper.client;

import javafx.geometry.Rectangle2D;
import javafx.stage.Screen;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 屏幕、鼠标信息
 */
public class ScreenInfo {

    private Robot robot;
    /**
     * 主屏幕
     */
    private Screen primaryScreenFx;
    /**
     * 屏幕尺寸
     */
    private Rectangle2D screenRectangleFx;

    private Toolkit toolkit;
    private GraphicsEnvironment graphicsEnvironment;
    private GraphicsDevice graphicsDevice;
    /**
     * 分辨率
     */
    private double dpi;

    public static ScreenInfo instance = new ScreenInfo();

    private ScreenInfo() {
        try {
            robot = new Robot();

            PointerInfo pointerInfo = MouseInfo.getPointerInfo();
            int numberOfButtons = MouseInfo.getNumberOfButtons();

            System.out.println("鼠标位置：" + pointerInfo.getLocation().getX() + "," + pointerInfo.getLocation().getY());
            System.out.println("鼠标按钮数：" + numberOfButtons);

            primaryScreenFx = Screen.getPrimary();
            screenRectangleFx = primaryScreenFx.getBounds();
            dpi = primaryScreenFx.getDpi();
            double[] screenZooms = scaleZoom();
            System.out.println("Fx幕分辨率dpi：" + dpi);
            System.out.println("屏幕缩放比,宽度比例=" + screenZooms[0] + ",高度比例=" + screenZooms[1]);

            //获取默认屏幕设备
            graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
            graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
            DisplayMode displayMode = graphicsDevice.getDisplayMode();

            toolkit = Toolkit.getDefaultToolkit();

            System.out.println("物理屏幕分辨率（每英寸的像素数）" + toolkit.getScreenResolution());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 计算屏幕缩放比例
     * @return 屏幕缩放比例数组[宽度缩放比例，高度缩放比例]
     */
    public double[] scaleZoom() {

        double logicalWidth = screenRectangleFx.getWidth();
        double logicalHeight = screenRectangleFx.getHeight();

        // 获取默认屏幕设备
        GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = environment.getDefaultScreenDevice();
        DisplayMode dm = gd.getDisplayMode();

        //设备物理宽度 / fx逻辑宽度
        double scaleX = (double) dm.getWidth() / logicalWidth;
        //设备物理高度 / fx逻辑高度
        double scaleY = (double) dm.getHeight() / logicalHeight;

        return new double[]{scaleX, scaleY};
    }

    public Robot getRobot() {
        return robot;
    }


    public Screen getPrimaryScreenFx() {
        return primaryScreenFx;
    }


    public Toolkit getToolkit() {
        return toolkit;
    }


    public GraphicsEnvironment getGraphicsEnvironment() {
        return graphicsEnvironment;
    }


    public GraphicsDevice getGraphicsDevice() {
        return graphicsDevice;
    }


    public double getDpi() {
        return dpi;
    }


    public Rectangle2D getScreenRectangleFx() {
        return screenRectangleFx;
    }

    /**
     * 截取屏幕
     *
     * @param rectangle
     * @return
     */
    public BufferedImage getScreen(Rectangle rectangle) {
        if (rectangle == null) {
            rectangle = new Rectangle(toolkit.getScreenSize());
        }
        return robot.createScreenCapture(rectangle);
    }

    /**
     * 获取鼠标指针位置
     *
     * @return
     */
    public PointerInfo getMousePointer() {
        return MouseInfo.getPointerInfo();
    }
}
